
<template>
  <!-- 这个自定义组件实现的功能是 数据懒加载和下拉刷新 -->
  <div>
    <el-table
      id="table"
      :data="filterTableData"
      style="width: 100%"
      border
      height="500"
    >
      <el-table-column type="index" />
      <el-table-column
        v-for="(value, key) in tableHeadMap"
        :label="key"
        :prop="value"
        align="center"
      ></el-table-column>
      <el-table-column align="right">
        <template #header>
          <el-input
            v-model="search"
            size="small"
            placeholder="Type to search"
          />
        </template>
        <template #default="scope" v-if="role == 'teacher'">
          <el-button size="small" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { deleteRecord } from "@/api/course";
import { mapGetters } from "vuex";
export default {
  props: ["cId", "studentList"],
  //这里应该包含传进来的参数 动态数据头 数据内容 当前课程ID
  data() {
    return {
      tableLayout: "auto",
      tableHeadMap: {
        学校: "school",
        姓名: "realName",
        性别: "gender",
        电话号: "tel",
        邮箱: "email",
      },
      search: "",
    };
  },
  computed: {
    filterTableData() {
      return this.studentList.filter(
        (data) =>
          !this.search ||
          data.realName.toLowerCase().includes(this.search.toLowerCase())
      );
    },
    ...mapGetters({
      role: "getRole",
    }),
  },
  methods: {
    handleDelete(student) {
      if (confirm("确认删除学生？")) {
        deleteRecord(this.cId, student.id).then((res) => {
          if (res.code == 200) {
            this.$emit('referStudents')
             ElMessage({
              type: "success",
              message: '删除成功',
            });
          } else {
            ElMessage({
              type: "error",
              message: res.reason,
            });
          }
        });
      } else {
        ElMessage({
          type: "info",
          message: "取消删除",
        });
      }
    },
    loadMore() {
      console.log("我到底部了");
    },
    load() {
      let dom = document.querySelector("#table").querySelector(".el-scrollbar");
      dom.addEventListener(
        "scroll",
        function () {
          const scrollDistance =
            dom.scrollHeight - dom.scrollTop - dom.clientHeight;
          console.log(dom.scrollHeight, dom.scrollTop, dom.clientHeight);
        },
        true
      );
    },
  },
};
</script>
<style scoped>
</style>